//
//  NSString+Test.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2023/11/23.
//  Copyright © 2023 moon. All rights reserved.
//

#import "NSString+Test.h"

@implementation NSString (Test)
- (void)test_enumSubString:(NSString *)substring block:(void(^)(NSRange range))block{
    if(substring.length==0)return;
    NSString *text = self;
    NSInteger location = 0;
    do{
        NSRange range = [text rangeOfString:substring options:NSCaseInsensitiveSearch range:NSMakeRange(location, text.length-location)];
        if(range.length>0){
            location = NSMaxRange(range);
            if(block){
                block(range);
            }
        }else{
            return;
        }
    }while(text!=nil);
}
@end

@implementation NSMutableAttributedString (Test)
- (void)test_setTextHighlightString:(NSString *)string color:(UIColor *)color backgroundColor:(UIColor *)backgroundColor tapAction:(YYTextAction)tapAction{
    NSString *text = self.string;
    [text test_enumSubString:string block:^(NSRange range) {
        [self yy_setTextHighlightRange:range color:color backgroundColor:backgroundColor?:UIColor.clearColor tapAction:tapAction];
    }];
}
- (void)test_setLinkString:(NSString *)string url:(nullable NSString *)url tapAction:(void(^ _Nullable)(NSString *string,NSString  * _Nullable url))tapAction{
    [self test_setTextHighlightString:string color:UIColor.systemBlueColor backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        if(tapAction){
            tapAction(string,url);
        }
    }];
}
@end
